package storeSorting

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/shopspring/decimal"
	"insighttracking/common/enum"
	"insighttracking/query/dao"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/sorting/internal/svc"
	"insighttracking/apps/sorting/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type CancelStoreSortingApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewCancelStoreSortingApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *CancelStoreSortingApiLogic {
	hostname, _ := os.Hostname()
	return &CancelStoreSortingApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *CancelStoreSortingApiLogic) CancelStoreSortingApi(req *types.CancelStoreSortingReq) (resp string, err error) {
	orderEntity, err := service.OrdersService.GetByIdApi(l.ctx, req.OrdersID)
	if err != nil {
		return "操作成功", nil
	}
	storeSortingEntity, err := service.StoreSortingService.GetStoreSortingByOrderIdApi(l.ctx, req.OrdersID)
	if err != nil {
		return "操作成功", nil
	}
	sortingDetailEntity, err := service.StoreSortingDetailService.GetStoreSortingDetailBySpuIDApi(l.ctx, req.OrdersID, req.SpuID)
	if err != nil {
		return "操作成功", nil
	}
	tx := dao.Use(l.svcCtx.MysqlDb).Begin()
	totalCount := storeSortingEntity.TotalCount
	amount := storeSortingEntity.TotalAmount
	for _, item := range sortingDetailEntity {
		if err = service.StoreSortingDetailService.DeleteByIdApi(l.ctx, tx, item.ID); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("取消失败")
		}
		totalCount = totalCount.Sub(item.OutboundCount)
		amount = amount.Sub(item.OutboundCount.Mul(item.SnapshotPrice))
	}

	// 分拣商品要减少
	if totalCount.LessThan(decimal.Zero) {
		return "", errors.New("取消失败")
	}
	if totalCount.Equal(decimal.Zero) {
		if err = service.StoreSortingService.DeleteByIdApi(l.ctx, tx, storeSortingEntity.ID); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("取消失败")
		}
	} else {
		if err = service.StoreSortingService.ModifyByIdApi(l.ctx, tx, storeSortingEntity.ID, totalCount, amount); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("取消失败")
		}
	}

	// 修改订单状态
	//if err = service.OrdersDetailService.ModifyStatusByIdListApi(l.ctx, tx, []int64{req.OrdersDetailId}, enum.OrdersCreateEnum); err != nil {
	//	if err = tx.Rollback(); err != nil {
	//		return "", err
	//	}
	//	return "", errors.New("取消失败")
	//}
	// 判断订单状态
	if orderEntity.Status == enum.OrdersSortingFinishEnum {
		if err = service.OrdersService.ModifyStatusByIdApi(l.ctx, tx, []int64{req.OrdersID}, enum.OrdersSortingProgressEnum); err != nil {
			if err = tx.Rollback(); err != nil {
				return "", err
			}
			return "", errors.New("取消失败")
		}
	}
	func(tx *dao.QueryTx) {
		if err = tx.Commit(); err != nil {

		}
	}(tx)

	return "操作成功", nil
}
